package com.wing.cabinet.controller.websocket.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wing.cabinet.controller.websocket.CabinetOpenServer;
import com.wing.cabinet.controller.websocket.service.BaseConnectService;
import com.wing.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.UUID;

/**
 * 接收小程序领新指令
 *  {"client":"applets","phone":"18942503919"，"cabCode":"SKU-ZG01-20221017-1","createTime":"2024-01-23 11:32:50.341","enterpriseCode":"Z0005","type":"inventoryOut","byPassword":true,"detailList":[{"cabCode":"SD-20231225-01","packId":10,"packName":"一盒一支","quantity":1,"quantityPackUnit":1,"skuCode":"A0016","spaceCode":"16","unitName":""}],"deviceCode":"YK-A105","processId":14,"workOrderId":12}
 */
@Service
@Slf4j
public class InventoryOutServiceImpl implements BaseConnectService {

    @Override
    public void logicMessage(String message) throws IOException {
        log.debug("收到消息:"+message);
        JSONObject jsonObject = JSONObject.parseObject(message);
        String type = jsonObject.getString("type"); //消息类型 codeSend
        String enterpriseCode = jsonObject.getString("enterpriseCode");  // 企业编号
        String cabCode = jsonObject.getString("cabCode"); //柜子编号
        String client = jsonObject.getString("client"); //客户端
        String phone = jsonObject.getString("phone"); //手机号

        String uuid = UUID.randomUUID().toString();

        jsonObject.put("uuid", uuid);
        jsonObject.put("client", "cab");
        try {
            CabinetOpenServer.sendMessage(enterpriseCode, cabCode, jsonObject.toJSONString());
        }catch (Exception e){
            throw new BusinessException("小程序领新转发失败:"+e.getMessage());
        }
        //JSONObject resultJsonObject = new JSONObject();
        //resultJsonObject.put("client", "applets");
        //resultJsonObject.put("phone", phone);
        //resultJsonObject.put("cabCode", cabCode);
        //resultJsonObject.put("createTime", LocalDateTimeUtils.formatNow(LocalDateTimeUtils.PATTERN));
        //resultJsonObject.put("enterpriseCode", enterpriseCode);
        //resultJsonObject.put("type", "inventoryOutResult");
        //resultJsonObject.put("deviceCode", "YK-A105");
        //
        //
        //JSONArray failJsonArray = new JSONArray();
        //JSONArray sussJsonArray = new JSONArray();
        //resultJsonObject.put("successList",sussJsonArray);
        //resultJsonObject.put("failList",failJsonArray);

        //CabinetOpenServer.sendAppletMessage(enterpriseCode, cabCode, phone, resultJsonObject.toJSONString());

    }


}
